﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="common/taglibs.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
    	<title>新增Job定义</title>
<jsp:include page="common/header.jsp"/>
		<script type="text/javascript">
			var param = new Object();
			var paraTypeName = new Object();
			
			function changeParams(cmd, loopVar) {
				var params = cmd.match(/\\${\w+}/g);
				$('#params').children().remove();
				if (params && params.length>0) {
					var exist = new Object();
					for (var p in params) {
						//检查参数是否重复
						if (params[p] == loopVar) {
							continue;
						} else if (exist[params[p]]) {
							continue;
						} else {
							exist[params[p]] = true;
						}

						var paramObj = param[params[p]];
						if (!paramObj) {
							param[params[p]] = new Object();
							paramObj = param[params[p]];
							paramObj.name=params[p];
							paramObj.define="none";
							paramObj.pattern='yyyy-MM-dd HH:mm:ss';
						}
						
						var line = $("#pLine").clone().removeAttr("id");
						$(line).find("#pName").text(paramObj.name);
						$(line).find("input[name='paraName']").val(paramObj.name);
						$(line).find("input[name='paraValue']").change(function(){
							var paramName=$(this).parent().parent().find("input[name='paraName']").val();
							param[paramName].pattern=$(this).val();
						}).val(paramObj.pattern);
						$(line).find("select").change(function(){
							if ($(this).val() == "none") {
								$(this).parent().find("#pPattern").hide();
							} else {
								$(this).parent().find("#pPattern").show();
							}
							var paramName=$(this).parent().find("input[name='paraName']").val();
							param[paramName].define=$(this).val();
						}).val(paramObj.define).change();
						$("#params").append(line);
						line.show();
					}
					$("#paramsLine").show();
					$(".sjc").show();
				} else {
					$("#paramsLine").hide();
					if ($('input[name=confType][checked=checked]:radio').val() != 'loop') {
						$(".sjc").hide();
					} else {
						$(".sjc").show();
					}
				}
			}
			
			function addJob(jid) {
				if (typeof jid != "string") jid = $("select[name=_job]").val();
				if (!jid) {
					alert ('请选择一个Job定义！');
					return false;
				}
				$.ajax({
					url: "ajax.action",
					async: false,
					data: "identifier=" + jid,
					dataType: "json",
					success: function(data){
						var line = $("#jobLine").clone().removeAttr("id");
						$(":first", line).text($("input[name=subJob]").size()+1);
						$(":eq(1)", line).text(data.obj.identifier.stringIdentifier);
						$(":eq(2)", line).text(data.obj.type);
						$(":eq(3)", line).text(data.obj.name);
						$(":eq(4)", line).text(data.obj.description);
						$(":eq(5)", line).html(getJobParameter(data.obj));
						$(line).find(":image[name=removeJob]").click(function(){
							line.remove();
							changeSubJobParams();
							$("input[name=subJob]").each(function(index){
								$(":first", $(this).parent().parent()).text(index+1);
							});
							return false;
						});
						$(line).find(":image[name=moveUpJob]").click(function(){
							var index = $(":first", line).html();
							if (index>1) {
								line.prev().before(line);
								changeSubJobParams();
	 							$("input[name=subJob]").each(function(index){
									$(":first", $(this).parent().parent()).text(index+1);
								});
							}
							return false;
						});
						$(line).find(":image[name=moveDownJob]").click(function(){
							var index = $(":first", line).html();
							var count = $("input[name=subJob]").size();
							if (index<count) {
								line.next().after(line);
								changeSubJobParams();
	 							$("input[name=subJob]").each(function(index){
									$(":first", $(this).parent().parent()).text(index+1);
								});
							}
							return false;
						});
						$(line).find("input[name=subJobPara]").keyup(changeSubJobParams);
						$("#subJobs").append(line);
						line.show();
						changeSubJobParams();
					}
				});
				return false;
			}
			
			function changeSubJobParams() {
				var vv = '';
				var loopVar = false;
				if ($('input[name=confType][checked=checked]:radio').val() == 'loop') {
					loopVar = $("input[name=loopVar]").val();
					vv += $("input[name=loopFrom]").val() + ',';
					vv += $("input[name=loopTo]").val() + ',';
				}
				$("input[name=subJobPara]").each(function(){
					vv += $(this).val() + ',';
				});
				changeParams(vv, loopVar);
			}
			
			function getJobParameter(conf) {
				var txt = '<input type=hidden name=subJob value="' + conf.identifier.stringIdentifier + '">';
				for (var i=0,size=conf.parameterSize;i<size;i++) {
					var p = conf.parameters[i];
					if (i>0) txt += '<BR>\n';
					txt += (i+1) + '. <b>' + p.name + '</b>=<input type=text name=subJobPara value="' + p.name + '">，预设值：' + paraTypeName[p.type];
					if (p.type != 'none') {
						txt += '，内容：' + p.value;
					}
				}
				return txt;
			}
			
			$(function(){
				//参数相关
				$("#pLine").hide();
				<c:forEach var="pn" items="${paraName}" varStatus="vs"><c:if test="${pn!=''}">
					param["${pn}"] = new Object();
					param["${pn}"].name = "${pn}";
					param["${pn}"].define = "${paraType[vs.index]}";
					param["${pn}"].pattern = "${paraValue[vs.index]}";
				</c:if></c:forEach>
				$('textarea[name="confContent"]').keyup(function(){
					changeParams($(this).val());
				});
				
				//类型切换相关
				$('input[name=confType]:radio').change(function(){
					$('._type').hide();
					$('._' + $(this).val()).show();
					if ($(this).val() == 'cmd') {
						$('textarea[name="confContent"]').keyup();
					} else {
						changeSubJobParams();
					}
				});
				
				//子任务相关
				<c:forEach var="pd" items="${defineTypes}">
					paraTypeName.${pd} = "${ss:getParameterDefineTypeName(pd)}";
				</c:forEach>
				$("#jobLine").hide();
				$(":image[name=addJob]").click(addJob);
				<c:forEach var="j" items="${subJob}">
					addJob("${j.stringIdentifier}");
				</c:forEach>
				//设置子Job参数
				var subJobPara = new Object();
				<c:forEach var="p" items="${subJobPara}" varStatus="vs">
					subJobPara[${vs.index}] = "${fn:replace(p,'\"', '\\"')}";
				</c:forEach>
				$("input[name=subJobPara]").each(function(idx, ele){
					$(this).val(subJobPara[idx]);
				});
				//设置子Job依赖
				var subJobDepend = new Object();
				<c:forEach var="p" items="${subJobDepend}" varStatus="vs">
					subJobDepend[${vs.index}] = "${p}";
				</c:forEach>
				$("input[name=subJobDepend]").each(function(idx){
					$(this).val(subJobDepend[idx]);
				});
				//设置子Job条件
				var subJobCondition = new Object();
				<c:forEach var="p" items="${subJobCondition}" varStatus="vs">
					subJobCondition[${vs.index}] = "${fn:replace(p,'\"', '\\"')}";
				</c:forEach>
				$("input[name=subJobCondition]").each(function(idx){
					$(this).val(subJobCondition[idx]);
				});
				//设置循环Job
				$("input[name=loopVar]").keyup(changeSubJobParams);
				$("input[name=loopFrom]").keyup(changeSubJobParams);
				$("input[name=loopTo]").keyup(changeSubJobParams);
				
				$('input[name=confType][value=${confType==null?"cmd":confType}]:radio').prop("checked", "checked").change();
				//设置任务过滤
				$("input[name=confFilter]").keyup(function(){
					var keys = $(this).val().split(/\s+/);
					$("select[name=_job] option").each(function(){
						for (k in keys) {
							if ($(this).text().indexOf(keys[k])<0) {
								$(this).hide();
								return;
							}
						}
						$(this).show().prop("selected","selected");
					});
				});
			});
		</script>
	</head>
	<body>
<jsp:include page="common/body-header.jsp"/>
		<form action="job-conf.action" method="post" class="yform">
		<b>基本信息</b>
		<table>
			<tr>
				<td align="right" width="100">Job名称：</td>
				<td><input type="text" name="confName" value="${confName}"></td>
			</tr>
			<tr valign="top">
				<td align="right">Job简介：</td>
				<td><textarea name="confDesc" cols="100" rows="3">${confDesc}</textarea></td>
			</tr>
			<tr>
				<td align="right">Job分类：</td>
				<td><select name="catalog"><c:forEach var="cat" items="${catalogs}">
					<option value="${cat.id}"<c:if test="${catalog==cat.id}"> selected="selected"</c:if>>${ss:getCatalogDeepName(catalogController, cat)}</option>
				</c:forEach></select></td>
			</tr>
		</table>
		<hr>
		<b>类型信息</b>
		<table>
			<tr>
				<td align="right" width="100">Job类型：</td>
				<td>
					<input type="radio" name="confType" value="cmd">命令行任务
					<input type="radio" name="confType" value="compound">复合任务
					<input type="radio" name="confType" value="loop">循环任务
				</td>
			</tr>
			<tr valign="top" class="_type _cmd">
				<td align="right">命令行：</td>
				<td><textarea name="confContent" cols="100" rows="3">${confContent}</textarea></td>
			</tr>
			<tr valign="top" class="_type _loop">
				<td align="right">循环参数：</td>
				<td>
					变量：<input type="text" name="loopVar" value="${loopVar}">
					，类型：<select name="loopType">
						<option value="int"<c:if test='${loopType=="int"}'> selected="selected"</c:if>>整数</option>
						<option value="day"<c:if test='${loopType=="day"}'> selected="selected"</c:if>>天</option>
						<option value="hour"<c:if test='${loopType=="hour"}'> selected="selected"</c:if>>小时</option>
						<option value="week"<c:if test='${loopType=="week"}'> selected="selected"</c:if>>周</option>
						<option value="month"<c:if test='${loopType=="month"}'> selected="selected"</c:if>>月</option>
					</select>
					，从：<input type="text" name="loopFrom" value="${loopFrom}">到：<input type="text" name="loopTo" value="${loopTo}">
					，步长：<input type="text" name="loopStep" value="${loopStep}" size="2">
					，<select name="loopDependency">
						<option value="true"<c:if test='${loopDependency}'> selected="selected"</c:if>>顺序执行</option>
						<option value="false"<c:if test='${!loopDependency}'> selected="selected"</c:if>>并发执行</option>
					</select>
				</td>
			</tr>
			<tr valign="top" class="_type _compound _loop">
				<td align="right">子Job：</td>
				<td><table id="subJobs">
					<tr>
						<th>No.</th>
						<th>Job Configuration Id</th>
						<th>Type</th>
						<th>Name</th>
						<th>Description</th>
						<th>Parameters</th>
						<th>Dependencies</th>
						<th class="sjc">Conditions</th>
						<th>&nbsp;</th>
					</tr>
					<tr id="jobLine">
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td><input type="text" name="subJobDepend"></td>
						<td class="sjc"><input type="text" name="subJobCondition"></td>
						<td>
							<input type="image" name="removeJob" value="Remove" src="images/icons/remove.jpg" title="删除子Job">
							<input type="image" name="moveUpJob" value="moveUp" src="images/icons/up.png" title="顺序提前">
							<input type="image" name="moveDownJob" value="moveDown" src="images/icons/down.png" title="顺序置后">
						</td>
					</tr>
				</table>
				<select name="_job" size="4" ><c:forEach var="c" items="${confs}">
					<option value="${c.identifier.stringIdentifier}">${c.identifier.stringIdentifier}:${c.name}:${c.description}</option>
				</c:forEach></select>
				过滤：<input type="text" name="confFilter">
				<input type="image" name="addJob" value="Add" src="images/icons/add.png" title="添加新的子Job"></td>
			</tr>
			<tr id="paramsLine" valign="top">
				<td align="right">参数：</td>
				<td><ol id="params"></ol></td>
			</tr>
			<tr valign="top" class="_type _cmd">
				<td align="right">服务器资源：</td>
				<td><input type="text" name="resource" value="${resource}"></td>
			</tr>
			<tr valign="top" class="_type _cmd">
				<td align="right">失败重试：</td>
				<td><select name="maxRetry">
					<option value="0"<c:if test="${maxRetry==0}"> selected="selected"</c:if>>不重试</option>
					<option value="1"<c:if test="${maxRetry==1}"> selected="selected"</c:if>>重试1次，延迟1分钟</option>
					<option value="2"<c:if test="${maxRetry==2}"> selected="selected"</c:if>>重试2次，延迟3分钟</option>
					<option value="3"<c:if test="${maxRetry==3}"> selected="selected"</c:if>>重试3次，延迟10分钟</option>
					<option value="4"<c:if test="${maxRetry==4}"> selected="selected"</c:if>>重试4次，延迟15分钟</option>
					<option value="5"<c:if test="${maxRetry==5}"> selected="selected"</c:if>>重试5次，延迟30分钟</option>
					<option value="6"<c:if test="${maxRetry==6}"> selected="selected"</c:if>>重试6次，延迟1小时</option>
					<option value="7"<c:if test="${maxRetry==7}"> selected="selected"</c:if>>重试7次，延迟1小时</option>
					<option value="8"<c:if test="${maxRetry==8}"> selected="selected"</c:if>>重试8次，延迟1小时</option>
					<option value="9"<c:if test="${maxRetry==9}"> selected="selected"</c:if>>重试9次，延迟1小时</option>
				</select></td>
			</tr>
		</table>
		<hr>
			<input type="hidden" name="action" value="createSubmit">
			<li id="pLine">参数名：<b id="pName"></b><input type="hidden" name="paraName">，预设值：<select name="paraType">
				<c:forEach var="pd" items="${defineTypes}">
					<option value="${pd}">${ss:getParameterDefineTypeName(pd)}</option>
				</c:forEach>
			</select><span id="pPattern">，内容：<input type="text" name="paraValue"></span></li>
			<c:if test="${message!=null}"><div style="color: red;">${message}</div></c:if>
			<input type="submit" value="创建">
		</form>
<jsp:include page="common/body-footer.jsp"/>
	</body>
</html>
